@echo off
setlocal enabledelayedexpansion

if "%1"=="" (
    echo Usage: %0 [api^|stream^|all]
    echo   api   - Build API server only
    echo   stream - Build stream server only
    echo   all   - Build both servers
    exit /b 1
)

set BUILD_TARGET=%1

:build_api
if "%BUILD_TARGET%"=="api" goto do_build_api
if "%BUILD_TARGET%"=="all" goto do_build_api
goto check_stream

:do_build_api
echo Compiling API server...
go build -o api_server.exe ./api

if %errorlevel% equ 0 (
    echo API server build successful! Executable created as api_server.exe
) else (
    echo API server build failed
    exit /b 1
)
goto check_stream

:check_stream
if "%BUILD_TARGET%"=="stream" goto do_build_stream
if "%BUILD_TARGET%"=="all" goto do_build_stream
goto end

:do_build_stream
echo Compiling stream server...
go build -o stream_server.exe ./streamserver

if %errorlevel% equ 0 (
    echo Stream server build successful! Executable created as stream_server.exe
) else (
    echo Stream server build failed
    exit /b 1
)
goto end

:end
if not "%BUILD_TARGET%"=="api" (
    if not "%BUILD_TARGET%"=="stream" (
        if not "%BUILD_TARGET%"=="all" (
            echo Invalid target: %BUILD_TARGET%
            echo Usage: %0 [api^|stream^|all]
            exit /b 1
        )
    )
)